/**
* Theme: Velonic Admin Template
* Author: Coderthemes
* Module/App: Dashboard Application
*/

  if (!window.Mage) var Mage = {};

    Mage.Cookies = {};
    Mage.Cookies.expires  = null;
    Mage.Cookies.path     = '/';
    Mage.Cookies.domain   = null;
    Mage.Cookies.secure   = false;
    Mage.Cookies.set = function(name, value){
         var argv = arguments;
         var argc = arguments.length;
         var expires = (argc > 2) ? argv[2] : Mage.Cookies.expires;
         var path = (argc > 3) ? argv[3] : Mage.Cookies.path;
         var domain = (argc > 4) ? argv[4] : Mage.Cookies.domain;
         var secure = (argc > 5) ? argv[5] : Mage.Cookies.secure;
         document.cookie = name + "=" + escape (value) +
           ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
           ((path == null) ? "" : ("; path=" + path)) +
           ((domain == null) ? "" : ("; domain=" + domain)) +
           ((secure == true) ? "; secure" : "");
    };

    Mage.Cookies.get = function(name){
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        var j = 0;
        while(i < clen){
            j = i + alen;
            if (document.cookie.substring(i, j) == arg)
                return Mage.Cookies.getCookieVal(j);
            i = document.cookie.indexOf(" ", i) + 1;
            if(i == 0)
                break;
        }
        return null;
    };

    Mage.Cookies.clear = function(name) {
      if(Mage.Cookies.get(name)){
        document.cookie = name + "=" +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
      }
    };

    Mage.Cookies.getCookieVal = function(offset){
       var endstr = document.cookie.indexOf(";", offset);
       if(endstr == -1){
           endstr = document.cookie.length;
       }
       return unescape(document.cookie.substring(offset, endstr));
    };




     function floatRemoveCompare(product_id){
         product_id = parseInt(product_id);
         removeCompare(product_id);

        jQuery('.link-compare input').each(function(){
            if(jQuery(this).val() == product_id){
                jQuery(this).attr("checked",false);
            }
        })
     }

      function removeCompare(product_id){
        var index = jQuery.inArray(product_id,compare_list);
        compare_list.splice(index,1);

        jQuery('.watch_compare .compare_list li').each(function(){
            var li_index = jQuery(this).attr('compare-index');
            if(li_index >= index && li_index!=4){
                jQuery(this).html(jQuery(this).next().html());
            }

            if(li_index == 4){
                jQuery(this).html('<p class="add_compare"><a href="#">添加</a></p>');
            }
        });

        Mage.Cookies.set('compare_list',compare_list.join(','),Mage.Cookies.expires,'/');
    }
/*compare end*/



      var compare_list_temp = new Array();
      var compare_list = new Array();

      if(Mage.Cookies.get('compare_list')){
        compare_list_temp = Mage.Cookies.get('compare_list').split(',');
        if(compare_list_temp){
            jQuery.each(compare_list_temp,function(n,value){
                compare_list.push(parseInt(value));
            });
            jQuery('.link-compare input').each(function(){
                var _index = jQuery.inArray(parseInt(jQuery(this).val()),compare_list);
                if(_index>=0){
                    jQuery(this).attr("checked",true);
                }
            })
        }
      }

      jQuery('.link-compare input').change(function(){
        var product_id = jQuery(this).val();
        product_id = parseInt(product_id);
        if(jQuery(this).is(':checked')){
            if(compare_list.length<5){
                var index = compare_list.length;
                compare_list.push(product_id);
                var img_src = jQuery(this).parent().find('input[name="compare-img"]').val();
                var li_html = '<a href=""><img src="'+img_src+'" width="68" height="68" alt="" /></a>'+
                        '<a class="close" href="javascript:;" onclick="floatRemoveCompare('+product_id+')"></a>';
                jQuery('.watch_compare .compare_list li:eq('+index+')').html(li_html);
            }else{
                alert('最多只能比较5个商品');
                jQuery(this).attr("checked",false);
            }
            Mage.Cookies.set('compare_list',compare_list.join(','),Mage.Cookies.expires,'/');
        }else{
            removeCompare(product_id);
        }
    })



